<?php
      //////////////////////////////////////////////////////////////////////
      //////////////////////////////////////////////////////////////////////
      //
      // Query khet1 all
      //
      //////////////////////////////////////////////////////////////////////
      //////////////////////////////////////////////////////////////////////
      //Get data from 5406 สอง
      $query = "SELECT 	tam_name, COUNT(aco_id) AS count_sungmen_all
                        FROM 
                          zikuladb.pn_degis_contact, zikuladb.`zk_votedatacenter_tambon` 
                        WHERE (aco_district_id =  5406 || aco_district_id =  5417)
                        AND tam_id = aco_tumbol_id
                        GROUP BY tam_name
                        ORDER BY tam_name";
      $column = array(
                "name", "count_sungmen_all"
                );

      $result = DBUtil::executeSQL($query);
      $alSungmenArray = DBUtil::marshallObjects ($result, $column);




      //Get data from 5406 สอง
      $query = "SELECT 	tam_name, COUNT(aco_id) AS count_sungmen_we
                        FROM 
                          zikuladb.pn_degis_contact, zikuladb.`zk_votedatacenter_tambon` 
                        WHERE (aco_district_id =  5406 || aco_district_id =  5417)
                        AND tam_id = aco_tumbol_id
                        AND aco_remarkv =1
                        GROUP BY tam_name
                        ORDER BY tam_name";
      $column = array(
                "name", "count_sungmen_we"
                );

      $result = DBUtil::executeSQL($query);
      $weSungmenArray = DBUtil::marshallObjects ($result, $column);




      //Get data from 5406 สอง
      $query = "SELECT 	tam_name, COUNT(aco_id) AS count_sungmen_they
                        FROM 
                          zikuladb.pn_degis_contact, zikuladb.`zk_votedatacenter_tambon` 
                        WHERE (aco_district_id =  5406 || aco_district_id =  5417)
                        AND tam_id = aco_tumbol_id
                        AND aco_remarkv = 0
                        AND aco_remarkv2 =1
                        GROUP BY tam_name
                        ORDER BY tam_name";
      $column = array(
                "name", "count_sungmen_they"
                );

      $result = DBUtil::executeSQL($query);
      $theySungmenArray = DBUtil::marshallObjects ($result, $column);




      //Get data from 5406 สอง
      $query = "SELECT 	tam_name, COUNT(aco_id) AS count_sungmen_unknown
                        FROM 
                          zikuladb.pn_degis_contact, zikuladb.`zk_votedatacenter_tambon` 
                        WHERE (aco_district_id =  5406 || aco_district_id =  5417)
                        AND tam_id = aco_tumbol_id
                        AND aco_remarkv =0
                        AND aco_remarkv2 =0
                        GROUP BY tam_name
                        ORDER BY tam_name";
      $column = array(
                "name", "count_sungmen_unknown"
                );

      $result = DBUtil::executeSQL($query);
      $unknownSungmenArray = DBUtil::marshallObjects ($result, $column);


      //Sungmen merge array
      for($i=0; $i<count($alSungmenArray); $i++){
        $compare = false;
        $compareThey = false;
        $compareUnknown = false;
        $alSungmenArrayName = $alSungmenArray[$i]['name'];
        $alSungmenArrayCount = $alSungmenArray[$i]['count_sungmen_all'];

        //Compare we
        for($j=0; $j<count($weSungmenArray); $j++){
          if($weSungmenArray[$j]['name'] == $alSungmenArrayName){
            //echo $weSungmenArray[$j]['aco_addr_moo']." with ".$alSungmenArray[$i]['aco_addr_moo']."<br>";
            $compare = true;
            $weSungmenValue = $weSungmenArray[$j]['count_sungmen_we'];
          }
        }

        //Compare they
        for($k=0; $k<count($theySungmenArray); $k++){
          if($theySungmenArray[$k]['name'] == $alSungmenArrayName){
            //echo $theySungmenArray[$k]['aco_addr_moo']." with ".$alSungmenArray[$i]['aco_addr_moo']."<br>";
            $compareThey = true;
            $theySungmenValue = $theySungmenArray[$k]['count_sungmen_they'];
          }
        }

        //Compare unknow
        for($l=0; $l<count($unknownSungmenArray); $l++){
          if($unknownSungmenArray[$l]['name'] == $alSungmenArrayName){
            //echo $unknownSungmenArray[$l]['aco_addr_moo']." with ".$alSungmenArray[$i]['aco_addr_moo']."<br>";
            $compareUnknown = true;
            $unknownSungmenValue = $unknownSungmenArray[$l]['count_sungmen_unknown'];
          }
        }

        if($compare == true && $compareThey == true && $compareUnknown == true){
            $reSungmenArray[$i] = array('name' => $alSungmenArrayName,
                                    'count_sungmen_all' => $alSungmenArrayCount,
                                    'count_sungmen_we' => $weSungmenValue,
                                    'count_sungmen_they' => $theySungmenValue,
                                    'count_sungmen_unknown' => $unknownSungmenValue
                              );
          $sum_count_sungmen_all += $alSungmenArrayCount;
          $sum_count_sungmen_we += $weSungmenValue;
          $sum_count_sungmen_they += $theySungmenValue;
          $sum_count_sungmen_unknown += $unknownSungmenValue;
          $sum_all_count_sungmen_unknown += $unknownSungmenValue;
        }else if($compare == false && $compareThey == true && $compareUnknown == true){
            $reSungmenArray[$i] = array('name' => $alSungmenArrayName,
                                    'count_sungmen_all' => $alSungmenArrayCount,
                                    'count_sungmen_we' => "0",
                                    'count_sungmen_they' => $theySungmenValue,
                                    'count_sungmen_unknown' => $unknownSungmenValue
                              );
          $sum_count_sungmen_all += $alSungmenArrayCount;
          $sum_count_sungmen_they += $theySungmenValue;
          $sum_count_sungmen_unknown += $unknownSungmenValue;
          $sum_all_count_sungmen_unknown += $unknownSungmenValue;
        }else if($compare == true && $compareThey == false && $compareUnknown == true){
            $reSungmenArray[$i] = array('name' => $alSungmenArrayName,
                                    'count_sungmen_all' => $alSungmenArrayCount,
                                    'count_sungmen_we' => $weSungmenValue,
                                    'count_sungmen_they' => "0",
                                    'count_sungmen_unknown' => $unknownSungmenValue
                              );
          $sum_count_sungmen_all += $alSungmenArrayCount;
          $sum_count_sungmen_we += $weSungmenValue;
          $sum_count_sungmen_unknown += $unknownSungmenValue;
          $sum_all_count_sungmen_unknown += $unknownSungmenValue;
        }else if($compare == true && $compareThey == true && $compareUnknown == false){
            $reSungmenArray[$i] = array('name' => $alSungmenArrayName,
                                    'count_sungmen_all' => $alSungmenArrayCount,
                                    'count_sungmen_we' => $weSungmenValue,
                                    'count_sungmen_they' => $theySungmenValue,
                                    'count_sungmen_unknown' => "0"
                              );
          $sum_count_sungmen_all += $alSungmenArrayCount;
          $sum_count_sungmen_we += $weSungmenValue;
          $sum_count_sungmen_they += $theySungmenValue;
        }else if($compare == false && $compareThey == true && $compareUnknown == true){
            $reSungmenArray[$i] = array('name' => $alSungmenArrayName,
                                    'count_sungmen_all' => $alSungmenArrayCount,
                                    'count_sungmen_we' => "0",
                                    'count_sungmen_they' => $theySungmenValue,
                                    'count_sungmen_unknown' => $unknownSungmenValue
                              );
          $sum_count_sungmen_all += $alSungmenArrayCount;
          $sum_count_sungmen_they += $theySungmenValue;
          $sum_count_sungmen_unknown += $unknownSungmenValue;
          $sum_all_count_sungmen_unknown += $unknownSungmenValue;
        }else if($compare == true && $compareThey == false && $compareUnknown == false){
            $reSungmenArray[$i] = array('name' => $alSungmenArrayName,
                                    'count_sungmen_all' => $alSungmenArrayCount,
                                    'count_sungmen_we' => $weSungmenValue,
                                    'count_sungmen_they' => "0",
                                    'count_sungmen_unknown' => "0"
                              );
          $sum_count_sungmen_all += $alSungmenArrayCount;
          $sum_count_sungmen_we += $weSungmenValue;
        }else if($compare == false && $compareThey == true && $compareUnknown == false){
            $reSungmenArray[$i] = array('name' => $alSungmenArrayName,
                                    'count_sungmen_all' => $alSungmenArrayCount,
                                    'count_sungmen_we' => "0",
                                    'count_sungmen_they' => $theySungmenValue,
                                    'count_sungmen_unknown' =>  "0"
                              );
          $sum_count_sungmen_all += $alSungmenArrayCount;
          $sum_count_sungmen_they += $theySungmenValue;
        }else if($compare == false && $compareThey == false && $compareUnknown == true){
            $reSungmenArray[$i] = array('name' => $alSungmenArrayName,
                                    'count_sungmen_all' => $alSungmenArrayCount,
                                    'count_sungmen_we' => "0",
                                    'count_sungmen_they' => "0",
                                    'count_sungmen_unknown' => $unknownSungmenValue
                              );
          $sum_count_sungmen_all += $alSungmenArrayCount;
          $sum_count_sungmen_none_canvasser += $unknownSungmenValue;
          $sum_all_count_sungmen_unknown += $unknownSungmenValue;
        }
      }

    //var_dump($reSungmenArray['count_sungmen_all']); exit;
    $render->assign('reportSungmen', "sungmen");
    $render->assign('district_sungmen', "อ.ร้องกวาง");
    $render->assign('sum_count_sungmen_all', $sum_count_sungmen_all);
    $render->assign('sum_count_sungmen_we', $sum_count_sungmen_we);
    $render->assign('sum_count_sungmen_they', $sum_count_sungmen_they);
    $render->assign('sum_count_sungmen_none_canvasser', $sum_count_sungmen_none_canvasser);
    $render->assign('sum_all_count_sungmen_unknown', $sum_all_count_sungmen_unknown);
    $render->assign('sum_count_sungmen_unknown', $sum_count_sungmen_unknown);
    $render->assign('objectSungmenArray', $reSungmenArray);

?>